/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import dao.CuaHangDAO;
import dao.LoaiNguoiDungDAO;
import dao.NguoiDungDAO;
import java.awt.Component;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import pojo.CuaHang;
import pojo.LoaiNguoiDung;
import pojo.NguoiDung;

/**
 *
 * @author 0
 */
public class DanhSachNhanVienGUI extends javax.swing.JPanel {

    /**
     * Creates new form DanhSachNhanVienGUI
     */
    private List<NguoiDung> nguoiDungs;
    
    public DanhSachNhanVienGUI() throws Exception {
        initComponents();
        SetTabOrder();
        AutoLoad();
    }
    
    private void SetTabOrder(){
        IndexedFocusTraversalPolicy indexedFocusTraversalPolicy=new IndexedFocusTraversalPolicy();
        indexedFocusTraversalPolicy.addIndexedComponents(new Component[]{txtMaNhanVien,txtTenNhanVien,txtUsername,txtCMND,cboLoaiNhanVien,cboCuaHang,btnTimKiem,btnThem,btnSua,btnXoa,btnClear});
        setFocusTraversalPolicy(indexedFocusTraversalPolicy);
    }
    
    private void AutoLoad() throws Exception{
        nguoiDungs=(List<NguoiDung>) NguoiDungDAO.LayDanhSach(NguoiDung.class,MainGUI.nguoiDung);
        Utility.CreateColumnsFromDB(NguoiDung.class, nguoiDungs, tbDanhSachNhanVien, new String[]{"Mã nhân viên","Tên nhân viên","Username","CMND","Điện thoại","Địa chỉ","Ngày sinh","Loại người dùng","Cửa hàng"}, new String[]{"password"});
        
        List<LoaiNguoiDung> loaiNguoiDungs=(List<LoaiNguoiDung>) LoaiNguoiDungDAO.LayDanhSach(LoaiNguoiDung.class,MainGUI.nguoiDung);
        loaiNguoiDungs.remove(0);
        Collections.sort(loaiNguoiDungs, new Comparator<LoaiNguoiDung>(){

            @Override
            public int compare(LoaiNguoiDung o1, LoaiNguoiDung o2) {
                return o1.getTenLoaiNguoiDung().compareTo(o2.getTenLoaiNguoiDung());              
            }
            
        });
        List<CuaHang> cuaHangs=(List<CuaHang>) CuaHangDAO.LayDanhSach(CuaHang.class,MainGUI.nguoiDung);
        Collections.sort(cuaHangs, new Comparator<CuaHang>(){

            @Override
            public int compare(CuaHang o1, CuaHang o2) {
                return o1.getTenCuaHang().compareTo(o2.getTenCuaHang());
            }
            
        });
        Utility.SetDataSourceInCombo(loaiNguoiDungs, cboLoaiNhanVien);
        Utility.SetDataSourceInCombo(cuaHangs, cboCuaHang);
        
        cboCuaHang.setRenderer(new DefaultListCellRenderer(){

            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if(value instanceof CuaHang){
                    CuaHang cuaHang=(CuaHang) value;
                    setText(cuaHang.getTenCuaHang());
                }
                return this;
            }
            
        });
        cboCuaHang.setSelectedIndex(-1);
        
        cboLoaiNhanVien.setRenderer(new DefaultListCellRenderer(){

            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if(value instanceof LoaiNguoiDung){
                    LoaiNguoiDung loaiNguoiDung=(LoaiNguoiDung) value;
                    setText(loaiNguoiDung.getTenLoaiNguoiDung());
                }
                return this;
            }
            
        });
        cboLoaiNhanVien.setSelectedIndex(-1);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtMaNhanVien = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtTenNhanVien = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        txtCMND = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        cboLoaiNhanVien = new javax.swing.JComboBox();
        jLabel6 = new javax.swing.JLabel();
        cboCuaHang = new javax.swing.JComboBox();
        btnTimKiem = new javax.swing.JButton();
        btnThem = new javax.swing.JButton();
        btnSua = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnClear = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbDanhSachNhanVien = new org.jdesktop.swingx.JXTable();

        jLabel1.setText("Mã nhân viên");

        jLabel2.setText("Tên nhân viên");

        jLabel3.setText("Username");

        jLabel4.setText("CMND");

        jLabel5.setText("Loại nhân viên");

        jLabel6.setText("Cửa hàng");

        btnTimKiem.setText("Tìm kiếm");

        btnThem.setText("Thêm");

        btnSua.setText("Sửa");

        btnXoa.setText("Xoá");

        btnClear.setText("Clear");

        tbDanhSachNhanVien.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbDanhSachNhanVien.setFont(new java.awt.Font("Times New Roman", 0, 14)); // NOI18N
        jScrollPane1.setViewportView(tbDanhSachNhanVien);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addGap(19, 19, 19)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtUsername, javax.swing.GroupLayout.DEFAULT_SIZE, 207, Short.MAX_VALUE)
                            .addComponent(txtMaNhanVien)
                            .addComponent(txtTenNhanVien))
                        .addGap(55, 55, 55)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel6))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtCMND)
                            .addComponent(cboLoaiNhanVien, 0, 207, Short.MAX_VALUE)
                            .addComponent(cboCuaHang, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(122, 122, 122)
                        .addComponent(btnTimKiem)
                        .addGap(18, 18, 18)
                        .addComponent(btnThem)
                        .addGap(18, 18, 18)
                        .addComponent(btnSua)
                        .addGap(18, 18, 18)
                        .addComponent(btnXoa)
                        .addGap(18, 18, 18)
                        .addComponent(btnClear)))
                .addGap(0, 40, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(txtMaNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(txtCMND, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtTenNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(cboLoaiNhanVien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(cboCuaHang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnTimKiem)
                    .addComponent(btnThem)
                    .addComponent(btnSua)
                    .addComponent(btnXoa)
                    .addComponent(btnClear))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 395, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClear;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnTimKiem;
    private javax.swing.JButton btnXoa;
    private javax.swing.JComboBox cboCuaHang;
    private javax.swing.JComboBox cboLoaiNhanVien;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXTable tbDanhSachNhanVien;
    private javax.swing.JTextField txtCMND;
    private javax.swing.JTextField txtMaNhanVien;
    private javax.swing.JTextField txtTenNhanVien;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
